package com.baidu.shop.service.impl;

import com.baidu.shop.base.Result;
import com.baidu.shop.dto.SkuDTO;
import com.baidu.shop.dto.SpecGroupDTO;
import com.baidu.shop.dto.SpecParamDTO;
import com.baidu.shop.dto.SpuDTO;
import com.baidu.shop.entity.SpecGroupEntity;
import com.baidu.shop.entity.SpecParamEntity;
import com.baidu.shop.entity.SpuDetailEntity;
import com.baidu.shop.feign.GoodsFeign;
import com.baidu.shop.feign.SpecFeign;
import com.baidu.shop.service.TemplatePageService;
import com.baidu.shop.utils.BaiduBeanUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @ClassName: TemplateServiceImpl
 * @Author: User
 * @date: 2020.09.23 19:26
 * @version: 1.0
 */
@Service
public class TemplatePageServiceImpl implements TemplatePageService {

    @Resource
    private GoodsFeign goodsFeign;

    @Resource
    private SpecFeign specFeign;

    @Override
    public Map<String, Object> getSpuParamById(Integer spuId) {
        Map<String, Object> map = new HashMap<>();
        //根据spuId查询spu
        SpuDTO spuDTO = new SpuDTO();
        spuDTO.setId(spuId);
        Result<PageInfo<SpuDTO>> resultSpu = goodsFeign.getSpuInfo(spuDTO);
        SpuDTO spuInfo = resultSpu.getData().getList().get(0);
        if (resultSpu.getData().getList().size() == 1) {
            map.put("spuInfo", spuInfo);
        }

        //获取spuDetil根据cid3
        SpuDetailEntity spuDetail = getSpuDetail(spuId);
        map.put("spuDetail", spuDetail);
        //获取规格组和通用参数组合By Cid
        List<SpecGroupDTO> groupsInParams = getGroupsInParams(spuInfo.getCid3());
        map.put("groupsInParams", groupsInParams);
        //获取特有规格参数
        Map<Integer, String> specParamMap = getSpecParamMap(spuInfo.getCid3());
        map.put("specParamMap", specParamMap);
        //获取sku
        List<SkuDTO> skuList = getSkuList(spuId);
        map.put("skuList",skuList);
        return map;
    }

    private List<SkuDTO> getSkuList(Integer spuId){
        Result<List<SkuDTO>> skuBySpuId = goodsFeign.getSkuBySpuId(spuId);
        if (skuBySpuId.getCode()==200){
            return skuBySpuId.getData();
        }
        return  null;
    }
    private Map<Integer,String> getSpecParamMap(Integer categoryId) {
        //规格参数-特有参数
        SpecParamDTO specParamDTO = new SpecParamDTO();
        specParamDTO.setCid(categoryId);
        specParamDTO.setGeneric(false);
        Map<Integer,String> specParams= new HashMap<>();
        Result<List<SpecParamEntity>> specParamInfo = specFeign.getSpecParamInfo(specParamDTO);
        if (specParamInfo.getCode()==200) {
            specParamInfo.getData().stream().forEach(specParam->{
                specParams.put(specParam.getId(),specParam.getName());
            });
        }
        return specParams;
    }

    private List<SpecGroupDTO> getGroupsInParams(Integer categoryId) {
        SpecGroupDTO spec = new SpecGroupDTO();
        spec.setCid(categoryId);
        Result<List<SpecGroupEntity>> sepcGroupInfo = specFeign.getSepcGroupInfo(spec);
        List<SpecGroupDTO> specGroupList = null;
        if (sepcGroupInfo.getCode() == 200) {
            List<SpecGroupEntity> data = sepcGroupInfo.getData();
            specGroupList = data.stream().map(specGroup -> {
                SpecGroupDTO specGroupDTO = BaiduBeanUtil.copyProperties(specGroup, SpecGroupDTO.class);
                //规格参数-通用参数
                SpecParamDTO specParamDTO = new SpecParamDTO();
                specParamDTO.setGeneric(true);
                specParamDTO.setGroupId(specGroupDTO.getId());
                Result<List<SpecParamEntity>> specParamInfo = specFeign.getSpecParamInfo(specParamDTO);
                if (specParamInfo.getCode() == 200) {
                    specGroupDTO.setSpecParamList(specParamInfo.getData());
                }
                return specGroupDTO;
            }).collect(Collectors.toList());
        }
        return specGroupList;
    }

    private SpuDetailEntity getSpuDetail(Integer spuId) {
        Result<SpuDetailEntity> spuDetailBydSpu = goodsFeign.getSpuDetailBydSpu(spuId);
        if (spuDetailBydSpu.getCode() == 200) {
            return spuDetailBydSpu.getData();
        }
        return null;
    }
}
